home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / code / cert / trk3_eg / error / fil_open / textedit.frm (.txt) < prev   
Encoding:
Visual Basic Form  |  1994-02-17  |  9.4 KB  |  295 lines

  1. VERSION 2.00
  2. Begin Form frmTextEdit 
  3.    Caption         =   "Text Editor"
  4.    ClientHeight    =   3405
  5.    ClientLeft      =   750
  6.    ClientTop       =   2100
  7.    ClientWidth     =   7680
  8.    Height          =   4095
  9.    Left            =   690
  10.    LinkMode        =   1  'Source
  11.    LinkTopic       =   "Form2"
  12.    ScaleHeight     =   3405
  13.    ScaleWidth      =   7680
  14.    Top             =   1470
  15.    Width           =   7800
  16.    Begin TextBox txtEditor 
  17.       FontBold        =   0   'False
  18.       FontItalic      =   0   'False
  19.       FontName        =   "MS Sans Serif"
  20.       FontSize        =   8.25
  21.       FontStrikethru  =   0   'False
  22.       FontUnderline   =   0   'False
  23.       Height          =   2535
  24.       Left            =   240
  25.       MultiLine       =   -1  'True
  26.       ScrollBars      =   3  'Both
  27.       TabIndex        =   0
  28.       Top             =   240
  29.       Width           =   2295
  30.    End
  31.    Begin Menu mnuFile 
  32.       Caption         =   "&File"
  33.       Begin Menu mnuFileNew 
  34.          Caption         =   "&New"
  35.       End
  36.       Begin Menu mnuFileOpen 
  37.          Caption         =   "&Open..."
  38.       End
  39.       Begin Menu mnuFileSave 
  40.          Caption         =   "&Save"
  41.       End
  42.       Begin Menu mnuFileSaveAs 
  43.          Caption         =   "Save&As..."
  44.       End
  45.       Begin Menu mnuSep1 
  46.          Caption         =   "-"
  47.       End
  48.       Begin Menu mnuFileExit 
  49.          Caption         =   "E&xit"
  50.       End
  51.    End
  52.    Begin Menu mnuEdit 
  53.       Caption         =   "&Edit"
  54.       Begin Menu mnuEditCut 
  55.          Caption         =   "Cu&t"
  56.          Enabled         =   0   'False
  57.          Shortcut        =   ^X
  58.       End
  59.       Begin Menu mnuEditCopy 
  60.          Caption         =   "&Copy"
  61.          Enabled         =   0   'False
  62.          Shortcut        =   ^C
  63.       End
  64.       Begin Menu mnuEditPaste 
  65.          Caption         =   "&Paste"
  66.          Enabled         =   0   'False
  67.          Shortcut        =   ^V
  68.       End
  69.       Begin Menu mnuSep2 
  70.          Caption         =   "-"
  71.       End
  72.       Begin Menu mnuEditClearClip 
  73.          Caption         =   "C&lear Clipboard"
  74.          Shortcut        =   ^L
  75.       End
  76.    End
  77. Option Explicit
  78. Sub Form_QueryUnload (cancel As Integer, UnloadMode As Integer)
  79.     'See if it's OK to nuke the user's changes (if any)
  80.     If Not OKtoNuke("Text has changed. Save before terminating?") Then
  81.         cancel = True
  82.     End If
  83. End Sub
  84. Sub Form_Resize ()
  85.     txtEditor.Move Me.ScaleLeft, Me.ScaleTop, Me.ScaleWidth, Me.ScaleHeight
  86. End Sub
  87. Sub Form_Unload (cancel As Integer)
  88.     'Terminate the application
  89.     End
  90. End Sub
  91. Sub mnuEdit_Click ()
  92.     Const CF_TEXT = 1
  93.     'Paste is only enabled if there's text in the paste buffer.
  94.     mnuEditPaste.Enabled = clipboard.GetFormat(CF_TEXT)
  95.     'Cut and copy are only enabled if something is selected.
  96.     If txtEditor.SelLength <> 0 Then
  97.         mnuEditCut.Enabled = True
  98.         mnuEditCopy.Enabled = True
  99.     Else
  100.         mnuEditCut.Enabled = False
  101.         mnuEditCopy.Enabled = False
  102.     End If
  103. End Sub
  104. Sub mnuEditClearClip_Click ()
  105.     'Clear the Clipboard
  106.     clipboard.Clear
  107. End Sub
  108. Sub mnuEditCopy_Click ()
  109.     'Place the selected text into the clipboard
  110.     clipboard.Clear
  111.     clipboard.SetText txtEditor.SelText
  112. End Sub
  113. Sub mnuEditCut_Click ()
  114.     clipboard.Clear
  115.     'Place the selected text into the clipboard
  116.     clipboard.SetText txtEditor.SelText
  117.     'Cut the selected text out of the source textbox
  118.     txtEditor.SelText = ""
  119. End Sub
  120. Sub mnuEditPaste_Click ()
  121.     'Place the clipboard text into the Active TextBox
  122.     txtEditor.SelText = clipboard.GetText()
  123. End Sub
  124. Sub mnuFileExit_Click ()
  125.     'See if it's OK to nuke the user's changes (if any)
  126.     If Not OKtoNuke("Text has changed. Save before terminating?") Then
  127.         Exit Sub
  128.     End If
  129.     'End Application
  130.     End
  131. End Sub
  132. Sub mnuFileNew_Click ()
  133.     'Make sure the file hasn't changed
  134.     If Not OKtoNuke("Text has changed. Save before opening a new file?") Then
  135.         Exit Sub
  136.     End If
  137.     'Reset the contents of the textbox
  138.     txtEditor.Text = ""
  139.     'Reset the file name to blank
  140.     fname = ""
  141.     'Reset the change flag to 0
  142.     dirtyflag = False
  143.     ' Reset the form caption
  144.     frmTextEdit.Caption = ""
  145. End Sub
  146. Sub mnuFileOpen_Click ()
  147.     Const ILLEGAL_FUNCTION_CALL = 5
  148.     Const OUT_OF_MEMORY = 7
  149.     Const NO_STRING_SPACE = 14
  150.     Dim KeyPressed As Integer
  151.     Dim fhandle As Integer
  152.     Dim mymsg As String
  153.     Dim msgtype As Integer
  154.     Dim msgtitle As String
  155.     'See if it's OK to nuke the user's changes (if any)
  156.     If Not OKtoNuke("Text has changed. Save before opening a new file?") Then
  157.         Exit Sub
  158.     End If
  159.     'Allow the user to select a file to open (or choose cancel)...
  160.     frmFileList.Show MODAL
  161.     'Make sure the user selected a file
  162.     If fname = "" Then Exit Sub
  163.     'You now have a filename
  164.     'Get a file number
  165.     fhandle = FreeFile
  166.     'Set the Open error handling trap
  167.     On Error GoTo OpenError
  168.     'Open the file for sequential input...
  169.     Open fname$ For Input As fhandle
  170.     'Change the mouse pointer to an hourglass
  171.     MousePointer = HOURGLASS
  172.     'Put file into text box
  173.     txtEditor.Text = Input$(LOF(fhandle), fhandle)
  174.     'Set the change flag indicator (no changes yet)
  175.     dirtyflag = False
  176.     'Set the form caption to the filename
  177.     frmTextEdit.Caption = fname$
  178. FileTooBig:
  179.     'Reset the mouse pointer back to normal
  180.     MousePointer = DEFAULT
  181.     'Close the file
  182.     Close fhandle
  183.     Exit Sub
  184. OpenError:
  185.     msgtype = RETRYCANCEL + WARNINGMESSAGE + SECONDBUTTON
  186.     msgtitle = "FILE SIZE ERROR"
  187.     'Determine the File Error
  188.     Select Case Err
  189.         Case ILLEGAL_FUNCTION_CALL
  190.             mymsg = "File's WAY too big."
  191.         Case OUT_OF_MEMORY
  192.             mymsg = "Not enough memory."
  193.         Case NO_STRING_SPACE
  194.             mymsg = "Out of string space."
  195.         Case Else    'Any other error
  196.             mymsg = "Some other error " + Str$(Err) + " " + Error$
  197.             msgtype = OK
  198.             msgtitle = "Unknown Error"
  199.     End Select
  200.             
  201.     KeyPressed = MsgBox(mymsg, msgtype, msgtitle)
  202.     Select Case KeyPressed
  203.         Case KEYRETRY               'Try again
  204.             Resume
  205.         Case KEYCANCEL              'Cancel attempt to read file
  206.             Resume FileTooBig
  207.         Case Else                   'Unexpected key value
  208.             mymsg = "Unexpected results, key = " + Str$(KeyPressed)
  209.             msgtype = CRITICAL
  210.             msgtitle = "KEY ERROR"
  211.             MsgBox mymsg, msgtype, msgtitle
  212.             'End the Application
  213.             End
  214.     End Select
  215. End Sub
  216. Sub mnuFileSave_Click ()
  217.     Dim fhandle As Integer
  218.     Dim Match As String
  219.     Dim ans As Integer
  220.     'If this is a new file with no filename, show the Save As Form...
  221.     If fname = "" Then
  222.         frmSaveAs.Show MODAL
  223.         'Make sure the user picked a filename
  224.         If fname = "" Then Exit Sub
  225.     End If
  226.     'If the file already exists, allow the user to not overwrite it...
  227.     Match = Dir$(fname)
  228.     If Match <> "" Then
  229.         ans = MsgBox("File, '" + fname + "' already exists.  Overwrite it?", YESNO + WARNINGQUERY + FIRSTBUTTON, "File Exists")
  230.         'Check the user response
  231.         If ans = KEYNO Then     ' User pressed no
  232.             Exit Sub
  233.         End If
  234.     End If
  235.     'Save the file:
  236.            
  237.     'Get a free file number, open the file for sequential output,
  238.     fhandle = FreeFile
  239.     Open fname For Output As fhandle
  240.            
  241.     'Write the contents of the text box to the file
  242.     Print #fhandle, txtEditor.Text
  243.            
  244.     'Close the file
  245.     Close fhandle
  246.     'Reset the change flag
  247.     dirtyflag = False
  248.     'Reset the form caption
  249.     frmTextEdit.Caption = fname
  250. End Sub
  251. Sub mnuFileSaveAs_Click ()
  252.     'Allow the user to pick a filename (or choose cancel)...
  253.     frmSaveAs.Show MODAL
  254.     'Check to see if user pressed cancel
  255.     If fname = "" Then Exit Sub
  256.     'Now, save the file...
  257.     mnuFileSave_Click
  258. End Sub
  259. Function OKtoNuke (mymsg As String) As Integer
  260.     Dim rc As Integer
  261.     Dim msgtype As Integer
  262.     Dim msgtitle As String
  263.     msgtype = YESNOCANCEL + WARNINGQUERY + FIRSTBUTTON
  264.     msgtitle = "Save Changes?"
  265.     'This function checks to see if the text has changed and then asks
  266.     'if changes should be saved before exiting, opening another,
  267.     'creating a new file, etc.
  268.     'It returns TRUE  == OK to lose the changes or
  269.     '            FALSE == not OK to lose the changes.
  270.     If dirtyflag Then
  271.         'File has changed, prompt user to save changes
  272.         rc = MsgBox(mymsg, msgtype, msgtitle)
  273.       
  274.         'Check which button user pressed. Set OKtoNuke based on user choice
  275.         Select Case rc
  276.             Case KEYCANCEL  'cancel was pressed
  277.                 'Don't nuke the file
  278.                 OKtoNuke = False
  279.             Case KEYYES      'yes was pressed
  280.                 mnuFileSave_Click
  281.                 OKtoNuke = Not dirtyflag
  282.             Case KEYNO       'no was pressed
  283.                 'Nuke the file
  284.                 OKtoNuke = True
  285.         End Select
  286.     Else
  287.         'The file has not changed, OK to Nuke it
  288.         OKtoNuke = True
  289.     End If
  290. End Function
  291. Sub txtEditor_Change ()
  292.     'Set the change flag - changes have occurred
  293.     dirtyflag = True
  294. End Sub
  295.